/**
 * Title: jFamilyTree provider
 * Description: jFamilyTree Provider
 * Version: provider
 * Copyright: (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 * Update: Mon Mar 10 00:00:00 CEST 2008
 */
package jFamilyTree.provider.xml.dom;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import org.xml.sax.SAXParseException;

/**
 * Parse error logger
 * 
 * @author Bernard Bou
 */
public class ParseErrorLogger extends ParseErrorHandler
{
	/**
	 * Printer
	 */
	private PrintWriter theWriter;

	/**
	 * Output stream
	 */
	private OutputStream theOutputStream;

	/**
	 * Constructor
	 */
	public ParseErrorLogger()
	{
	}

	/**
	 * Log
	 * 
	 * @param thisLevel
	 *        level
	 * @param e
	 *        exception
	 */
	private void log(String thisLevel, SAXParseException e)
	{
		try
		{
			if (theOutputStream == null)
				theOutputStream = new FileOutputStream("parse.log");
			if (theWriter == null)
				theWriter = new PrintWriter(theOutputStream, true);

			theWriter.println(thisLevel);
			theWriter.println(" uri: " + e.getSystemId() + "(" + e.getLineNumber() + "," + e.getColumnNumber() + ")");
			theWriter.println(" message: " + e.getMessage());
		}
		catch (IOException ole)
		{
			theOutputStream = null;
		}
	}

	/**
	 * Terminate
	 */
	public void terminate()
	{
		try
		{
			if (theWriter != null && (theFatalErrors != 0 || theErrors != 0 || theWarnings != 0))
			{
				theWriter.println("Fatal Errors:" + theFatalErrors);
				theWriter.println("Errors:" + theErrors);
				theWriter.println("Warnings:" + theWarnings);
			}
			if (theOutputStream != null)
				theOutputStream.close();
		}
		catch (IOException ole)
		{
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.provider.xml.dom.ParseErrorHandler#error(org.xml.sax.SAXParseException)
	 */
	public void error(SAXParseException e) throws SAXParseException
	{
		super.error(e);
		log("Recoverable Error", e);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.provider.xml.dom.ParseErrorHandler#warning(org.xml.sax.SAXParseException)
	 */
	public void warning(SAXParseException e) throws SAXParseException
	{
		super.warning(e);
		log("Warning", e);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.provider.xml.dom.ParseErrorHandler#fatalError(org.xml.sax.SAXParseException)
	 */
	public void fatalError(SAXParseException e) throws SAXParseException
	{
		super.fatalError(e);
		log("FATAL ERROR", e);
	}
}
